package com.akk.module.user.service;


import com.akk.module.user.entity.User;
import com.akk.module.user.entity.UserExample;
import com.akk.module.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@CacheConfig(cacheNames="user")
@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserExample userExample;

    @Cacheable
    public User findByPhoneAndAppId(String phone, String appId) {
        userExample.clear();
        userExample.createCriteria().andPhoneEqualTo(phone).andAppIdEqualTo(appId);
        List<User> users = userMapper.selectByExample(userExample);
        return users.isEmpty()?null:users.get(0);
    }
    @CacheEvict(allEntries = true)
    public void insert(User user) {
        userMapper.insert(user);
    }
    @CacheEvict(allEntries = true)
    public void update(User user) {
        userMapper.updateByPrimaryKey(user);
    }
    @Cacheable
    public List<User> findByAppIdAndSpreadIds(String appId, List<String> userIds) {
        userExample.clear();
        userExample.createCriteria().andAppIdEqualTo(appId).andSpreadIdIn(userIds);
        return userMapper.selectByExample(userExample);
    }
    @Cacheable
    public User findByUserId(String userId) {
        return userMapper.selectByPrimaryKey(userId);
    }
}
